// Copyright (C) 2006-2007 - Ivan Zhakov (chemodax@gmail.com)
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

using System;
using System.Collections;
using System.IO;

namespace CTCDecoder
{
    public class CTCAnalyzer
    {
        class Node
        {
            public IDPair ID;
            public string Name = "";
            public ArrayList Childs = new ArrayList();

            public Node(IDPair id, string name)
            {
                this.ID = id;
                this.Name = name;
            }
        };
        
        private Node root; 
        
        public CTCAnalyzer()
        {
            this.root = new Node(new IDPair(), "");
        }

        static Node FindByGuid(Node root, IDPair itemID)
        {
            foreach(Node node in root.Childs)
            {
                // if (node.ID.GUID == itemID.GUID && node.ID.ID == itemID.ID)
                if (node.ID == itemID)
                    return node;

                Node child = FindByGuid(node, itemID);
                if (child != null)
                    return child;
            }
            return null;
        }

        static void PrintNode(Node node, int level, TextWriter stream)
        {
            for (int i = 0; i < level; i++)
                stream.Write("  ");

            stream.WriteLine("{0} {1}",
                node.ID.ToString(),
                node.Name);
            
            foreach(Node child in node.Childs)
            {
                PrintNode(child, level + 1, stream);
            }
        }

        public void Print(TextWriter stream)
        {
            foreach(Node child in root.Childs)
            {
                PrintNode(child, 0, stream);
            }
        }

        public void AddNode(IDPair itemID, IDPair parentID, string name)
        {
            Node parent = FindByGuid(root, parentID);
            if (parent == null)
            {
                parent = new Node(parentID, "");
                root.Childs.Add(parent);
            }

            Node node = FindByGuid(root, itemID);
            if (node == null)
            {
                node = new Node(itemID, name);
            }
            else
            {
                if (root.Childs.IndexOf(node) != -1)
                {
                    root.Childs.Remove(node);
                }
                
                if (name != String.Empty)
                    node.Name = name;
            }

            parent.Childs.Add(node);
        }
    }
}
